import {aspect, DbContext} from '@codefree/badger/service'

export default function (pattern) {
    let options = {};
    if (pattern) {
        options.methodPattern = pattern;
    }

    options.handle = (metadata) => {
        const req = metadata.target.req;
        DbContext.OperationLog.create({
            optType: metadata.className,
            optCode: `${metadata.className}.${metadata.methodName}`,
            businessData: JSON.stringify(metadata.args),
            inUserId: req.user ? req.user.id : 0,
            inUserName: req.user ? req.user.username : 'unknown',
        })
    };
    return aspect.beforeMethodOnClass(options)
}